package com.mmlc.businessutils;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.meimei.lichengkongjian.Adapter.AllOrderAdapter;
import com.mmlc.network.filter.OrderListRequestFilter;
import com.mmlc.network.responsebean.OrderListResponseBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/23 0023.
 */
public class OrderShowUtils{

    BaseParentActivity context;
    PullToRefreshListView pullRefreshList;
    public List<OrderListResponseBean.DataBean> item = new ArrayList<>();
    int type ;
    public Object object;
    public OrderShowUtils(Object object, BaseParentActivity context, PullToRefreshListView pullRefreshList){
        this.context = context;
        this.pullRefreshList = pullRefreshList;
        this.object = object;
    }

    public ViewGroup rootView;
    public void showOrder(int type, ViewGroup rootView){
        this.type = type;
        this.rootView = rootView;
        initListView(pullRefreshList);
        requestDataForList();
    }

    public PullToRefreshUtils pullUtils;
    private void initListView(final PullToRefreshListView pullRefreshList) {
        pullUtils = new PullToRefreshUtils(pullRefreshList, new PullToRefreshUtils.RefreshListener<OrderListResponseBean.DataBean>() {

            @Override
            public boolean compareFirstItem(OrderListResponseBean.DataBean adapterFirstItem) {
                String resultId = curResult.getData().get(0).getId();
                String currId = item.get(0).getId();
                return currId.equals(resultId);
            }

            @Override
            public void processList(List<OrderListResponseBean.DataBean> list) {
                processAdapterList();
            }

            @Override
            public void refresh(boolean isPull) {
                requestDataForList();
            }

            @Override
            public List<OrderListResponseBean.DataBean> getAdapterList() {
                return item;
            }

            @Override
            public List<OrderListResponseBean.DataBean> getRequestList() {
                return curResult.getData();
            }
        });
    }

    OrderListRequestFilter orderListFilter;
    OrderListResponseBean curResult;
    public boolean isClearAdapterList = false;
    public boolean isNeedTransparent = false;
    public void requestDataForList() {

        if(orderListFilter == null){
            orderListFilter = new OrderListRequestFilter(context);
            orderListFilter.requestBean.method = "orderList";
            if (type<5)
            orderListFilter.requestBean.type = type;
            else if (type==5)
                orderListFilter.requestBean.order_type = 1;
            else if (type==6)
                orderListFilter.requestBean.order_type = 2;
        }
        if(isNeedTransparent){
            orderListFilter.isTransparence = true;
        }else{
            isNeedTransparent = true;
            orderListFilter.isTransparence = false;
        }
        orderListFilter.offerErrorParams(rootView);

        orderListFilter.requestBean.pageNow = pullUtils.currPageIndex;

        orderListFilter.requestBean.pageSize = pageSize;
        orderListFilter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<OrderListResponseBean>() {

            @Override
            public void onSuccess(OrderListResponseBean result) {
                curResult = result;
                isNeedTransparent = false;
                if(isClearAdapterList){
                    isClearAdapterList = false;
                    item.clear();
                }
                pullUtils.recordRequest(orderListFilter,true);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                isNeedTransparent = false;
                pullUtils.recordRequest(orderListFilter,false);
            }
        });
    }

    public int pageNow = 0;
    public int pageSize = 10;
    public boolean isPUll = true;
    boolean isNeedIncrement = true;
    public AllOrderAdapter orderListAdapter;
    public int requestType = -1;
    private void processAdapterList() {

        if(orderListAdapter == null){
            orderListAdapter = new AllOrderAdapter(OrderShowUtils.this,rootView,item,context);
            pullRefreshList.setAdapter(orderListAdapter);
        }else{
            orderListAdapter.notifyDataSetChanged();
        }
    }

    public static void detailFinish(int requestCode,Intent data,OrderShowUtils showUtils){
        if(requestCode == 1){
            if(data != null){
                int state = data.getIntExtra("state", -1);
                if(state == 1){//需要刷新
                    showUtils.item.clear();
                    showUtils.orderListAdapter.notifyDataSetChanged();
                    showUtils.pullUtils.myRefreshListener.onPullDownToRefresh(null);
                }
            }
        }
    }
}
